package top.yongyan0624.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yongyan0624.mapper.StudentMapper;
import top.yongyan0624.pojo.Student;
import top.yongyan0624.service.StudentService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2020/12/23
 **/

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    HashMap<String, Integer> hashMap;

    public Integer addStu(Student student) {
        return studentMapper.addStu(student);
    }

    public Integer deleteStu(Integer id) {
        return studentMapper.deleteStu(id);
    }

    public Integer updateStu(Student student) {
        return studentMapper.updateStu(student);
    }

    public PageInfo<Student> findAllStudent(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Student> studentList = studentMapper.findAllStudent();

        return new PageInfo<Student>(studentList);
    }

    public Student findById(Integer id) {
        return studentMapper.findById(id);
    }

    public List<Student> findByName(String name) {
        return studentMapper.findByName(name);
    }

    public List<Student> findStu(Student student) {
        return studentMapper.findStu(student);
    }

    public Integer totalStu() {
        return studentMapper.totalStu();
    }

    public List<Student> findSelectById(Integer id) {
        return studentMapper.findSelectById(id);
    }

    public List<Integer> findSelectIdById(Integer id) {
        return studentMapper.findSelectIdById(id);
    }

    public void selectLesson(List<Integer> selectList,Integer id) {
        studentMapper.deleteLesson(id);

        List<Integer> selectById = findSelectIdById(id);

        for (Integer select : selectList) {
            if (!selectById.contains(select)){
                hashMap.put("id",id);
                hashMap.put("select",select);
                studentMapper.selectLesson(hashMap);
            }
        }
    }

}
